package com.koron.ticket.work.workyccontent.service;

import cn.hutool.core.collection.CollUtil;
import com.koron.common.core.util.CodeTools;
import com.koron.ticket.work.workyccontent.bean.YcContentBean;
import com.koron.ticket.work.workyccontent.mapper.YcContentBeanMapper;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class YcContentService {

    public void batchInsert(SessionFactory factory, List<YcContentBean> list) {
        YcContentBeanMapper mapper = factory.getMapper(YcContentBeanMapper.class);
        list.stream().forEach(l->l.setId(CodeTools.getCode32()));
        mapper.batchInsert(list);
    }

    public void save(SessionFactory factory, List<YcContentBean> list, String ticketId, String ticketType) {
        YcContentBeanMapper mapper = factory.getMapper(YcContentBeanMapper.class);
        mapper.deleteByWorkTicketId(ticketId);
        if (CollUtil.isEmpty(list)) {
            return;
        }
        list.stream().forEach(l -> {
            l.setId(CodeTools.getCode32());
            l.setWorkTicketId(ticketId);
            l.setTicketType(ticketType);
        });
        mapper.batchInsert(list);
        return;
    }

    public List<YcContentBean> getYcInfolist(SessionFactory factory, String workTicketId) {
        YcContentBeanMapper mapper = factory.getMapper(YcContentBeanMapper.class);
        List<YcContentBean> list = mapper.queryByWorkTicketId(workTicketId);
        return list;
    }

    public void deleteByWorkTicketId(SessionFactory factory, String workTicketId) {
        YcContentBeanMapper mapper = factory.getMapper(YcContentBeanMapper.class);
        mapper.deleteByWorkTicketId(workTicketId);
    }
}
